package hxy.scan.server.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import hxy.scan.server.dao.mapper.GoodsMapper;
import hxy.scan.server.dao.model.GoodsModel;
import hxy.scan.server.entity.BaseResponse;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 通常代码逻辑应该写这里
 */
@Service
public class GoodsService extends ServiceImpl<GoodsMapper, GoodsModel> {

    @Resource
    GoodsMapper goodsMapper;

    /**
     * 增加
     */
    BaseResponse add(GoodsModel goodsModel) {
        goodsMapper.insert(goodsModel);
        return BaseResponse.success();
    }


    /**
     * 删除
     */
    BaseResponse delete(GoodsModel goodsModel) {

        return BaseResponse.success();
    }

    /**
     * 修改
     */
    BaseResponse update(GoodsModel goodsModel) {

        return BaseResponse.success();
    }

    /**
     * 查询
     */
    BaseResponse get(GoodsModel goodsModel) {

        return BaseResponse.success();
    }

    /**
     * 查询所有
     */
    BaseResponse list(GoodsModel goodsModel) {

        return BaseResponse.success();
    }

    /**
     * 批量更新所有
     */
    public BaseResponse saveOrUpdateBatchList(List<GoodsModel> goodsModels) {
        if (!CollectionUtils.isEmpty(goodsModels)) {
            goodsModels.stream().forEach(goodsModel -> {

                GoodsModel goodsModel1 = goodsMapper.selectById(goodsModel.getNumber());
                if (goodsModel1 != null) {
                    // 判断时间
                    Long updateTime1 = goodsModel1.getUpdateTime();
                    Long updateTime = goodsModel.getUpdateTime();
                    if (updateTime != null && updateTime1 != null && updateTime > updateTime1) {
                        //更新到数据库
                        goodsMapper.updateById(goodsModel);
                    }
                } else {
//                    直接插入到数据库
                    goodsMapper.insert(goodsModel);
                }
            });
            // 数据库查询是否存在

        }
        return BaseResponse.success();
    }
}
